package com.gzdh.device.wx.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gzdh.device.comm.vo.Result;
import com.gzdh.device.wx.entity.BillDatasource;
import com.gzdh.device.wx.service.BillDatasourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/bill/datasource/v1")
public class BillDatasourceController {

    @Autowired
    BillDatasourceService billDatasourceService;


    /**
     * 获取对账单数据源
     *
     * @return
     */
    @GetMapping("getDataSourcePage")
    public Result<Object> getDataSourcePage(@RequestParam("merchantId") String merchantId) {
        Result<Object> result = new Result<>();
        List<BillDatasource> billDatasourceList = billDatasourceService.list(new QueryWrapper<BillDatasource>().lambda().eq(BillDatasource::getMerchantId, merchantId));
        result.setResult(billDatasourceList);
        return result;
    }

    @PostMapping("addDataSource")
    public Result<Object> addDataSource(@RequestBody BillDatasource billDatasource) {
        Result<Object> result = new Result<>();
        int counts = billDatasourceService.count(new QueryWrapper<BillDatasource>().lambda().eq(BillDatasource::getMerchantId, billDatasource.getMerchantId())
                .eq(BillDatasource::getDatasourceName, billDatasource.getDatasourceName()));
        if (counts > 0) {
            return Result.error("添加失败");
        }
        billDatasource.setId(null);
        billDatasourceService.save(billDatasource);
        return result;
    }


}
